var DWZ=parent.DWZ;
var choiceEntitys={};
$(document).ready(function(){	
//	getList();	
	getSign();
});

//获取Sign列表
function getSign(){
	var url = "/management/sign";
	var sel = $("#typeSelectDom");
	sel.html("");
	parent.DWZ.get(url,function(json){
		if(!json) return;
		var data=json.result||[];
		$.each(data,function(i,d){
			var op = new Option(d.value,d.sign);
			$(op).attr("action",d.name+"/category");
			sel[0].options.add(op);
		});	
	});
}

//获取Sign对应的分组列表
function selectSign(sel){
	var url=$(sel.options[sel.selectedIndex]).attr("action");
	var viewSelect = $("#viewSelectDom");
	viewSelect.html("");
	var html=[];
	parent.DWZ.get(url,function(json){
		if(!json) return;
		var data = json.result||[];
		if(data.length==0){
			viewSelect.append("<a href='javascript:void(0);'>无相关列表</a>");
		}
		$.each(data,function(i,d){
			var temp = $("#viewSelectTemplet").html();
			temp = temp.replace(/\${title}/g,d.name).replace(/\${category_id}/g,d.category_id)
					.replace(/\${sign}/g,d.sign);
			html.push(temp);
		});
		viewSelect.append(html.join(""));
		$("#viewSelectDom").children("li").children("a").first().click();
	});
}

/**
 * 执行选择操作
 */
function choice(){
	if(callback)callback(choiceEntitys);
}

function selectView(sel){
	var url="/demo/view";
	var sign=$(sel).attr("sign");
	parent.DWZ.get(url,{sign:sign},function(view){
		var viewContainer = $("#view");
		viewContainer.html("");
		var data = view.result||[];
		if(data.length==0){
			$(viewContainer).append("无相关模板");
		}
		$.each(data,function(i,d){
			url = "category/"+$(sel).attr("categoryId")+"/entity";
			parent.DWZ.get(url,function(entity){
				var entitys = entity.result||[];
				bindView(viewContainer,d,entitys);
			});
		});
	});
}

//绑定列表数据
function bindView(viewContainer,view,entitys){
	var templet = view.templet;
	if(entitys==undefined || entitys.length==0){
		$(viewContainer).html("").append("无相关数据");
		return ;
	}
	$.each(entitys,function(i,entity){
		var html = [];
		var row = templet.bind(entity);
		html.push(row);
		var container = view.container.replace(/\$\{container}/,html.join(""));
		var checkbox = $("<div name='head' style='text-align: left;'><span><input type='checkbox' name='view' value='"+view.view_id+"' /></span></div>");
		checkbox.appendTo(viewContainer);
		$(container).appendTo(viewContainer);
		checkbox.find("input[type=checkbox]").click(function(e){
				if(this.checked){
				choiceEntitys["entity_id"+entity.entity_id]={
					data:entity,
					demo:container
				}
				$(this).parents("div[name=head]").next().css("border","red 1px solid");
			}else{
				delete choiceEntitys["entity_id"+entity.entity_id];
				$(this).parents("div[name=head]").next().css("border","red 0px solid");
			}
		});
	});
}